package com.lhm.service;



import com.lhm.entity.Order;
import com.lhm.entity.Orditem;
import com.lhm.mapper.BookMapper;
import com.lhm.mapper.CartMapper;
import com.lhm.mapper.OrderMapper;

import com.lhm.z.ItemBook;
import com.lhm.z.OrdInfo;
import com.lhm.z.OrderLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;


@Primary
@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CartMapper cartMapper;
    @Autowired
    BookMapper bookMapper;


    @Transactional
    @Override
    //因为购物车转订单涉及到两次的 SQL 操作，所以要写在service层，需要启用事务保护的
    //首先要计算出金额多少，然后判断订单是否为空，再获取订单编号 id（ordid）判断是否插入订单成功，通过迭代器遍历
    public void addOrdItem(Order order, List<Orditem>items) {
        double amt = 0;
        Orditem item;
        //金额=数量+价格
        for(int i=0;i<items.size();i++){
            item=items.get(i);
            amt+=(double) item.getNum() * item.getPrice();
            System.err.println(amt);
        }
        //判断一下订单是否添加成功
        if (amt!=0){
            order.setAmount(amt);
            orderMapper.addOrd(order);
        }
        int ordid = order.getOrdid(); //获取 ordid
        if(ordid<1){
            throw new RuntimeException("插入订单失败, 终止事务"); //抛出异常
        }else{
            Iterator iterator = items.iterator();
            while(iterator.hasNext()) {//遍历处理信息
                Orditem orditem = (Orditem)iterator.next();
                orditem.setOrdid(ordid);
               cartMapper.remove(orditem.getItemid());//删除购物车
               orderMapper.addItm(orditem); //添加订单明细
               bookMapper.addFavnum(orditem.getBookid(), orditem.getNum()); //添加销量数量
            }


        }
    }





    @Override
    public void updateOrdAddr(Order ord) {
            orderMapper.updateOrdAddr(ord);
    }

    @Override
    public void updateOrdPay(Order ord) {
            orderMapper.updateOrdPay(ord);
    }

    @Override
    public Order findOrderById(int ordid) {
        return orderMapper.findOrderById(ordid);
    }

    @Override
    public List<OrderLine> findOrdLines(Map<String, Object> map) {
        return orderMapper.findOrdLines(map);
    }

    @Override
    public int getCount(Map<String, Object> map) {
        return orderMapper.getCount(map);
    }

    @Override
    public List<Map<String, Object>> findItemInfoByOrdid(int ordid) {
        return orderMapper.findItemInfoByOrdid(ordid);
    }

    @Override
    public void updateOrdShip(Order ord) {
        orderMapper.updateOrdShip(ord);
    }

    @Override
    public OrdInfo findInfoByOrdid(int uid, int ordid) {
        Map<String, Object> map = new HashMap();
        map.put("uid", uid);
        map.put("ordid", ordid);
        return orderMapper.findInfoByOrdid(map);
    }

    @Override
    public List<ItemBook> findItemBookByOrdid(int ordid) {
        return orderMapper.findItemBookByOrdid(ordid);
    }

    @Override
    public List<OrdInfo> findOrdInfo(int uid, int ispay, Date tstart, Date tend) {
        return orderMapper.findOrdInfo(uid, ispay, tstart, tend);
    }
}
